package com.kly.chatter.api.application.service.impl;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.kly.chatter.api.application.controller.user.req.MessageReq;
import com.kly.chatter.api.application.controller.user.res.ModelRes;
import com.kly.chatter.api.application.controller.user.res.UserRes;
import com.kly.chatter.api.application.service.UsersService;
import com.kly.chatter.api.application.utils.Result;
import com.kly.chatter.db.dao.ChatterOnlineListMatchRecordDao;
import com.kly.chatter.db.entity.ChatterOnlineListMatchRecordEntity;
import com.kly.user.dto.ModelDto;
import com.kly.user.dto.UserDialogDto;
import com.kly.user.service.UserService;
import com.kly.user.service.WhiteListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UsersServiceImpl implements UsersService {


    @NacosValue(value = "${chatter.max.send.flashchat.cnt:5}", autoRefreshed = true)
    private Integer chatterMaxSendFlashchatPeriod;

    @NacosValue(value = "${chatter.max.send.flashchat.switch:false}", autoRefreshed = true)
    private boolean chatterMaxSendFlashchatSwitch;

    @Resource
    private UserService userService;

    @Autowired
    private ChatterOnlineListMatchRecordDao chatterOnlineListMatchRecordDao;
    @Resource
    private WhiteListService whiteListService;

    @Override
    public List<UserRes> getUsers(String chatterUserCode) {
        boolean isSendFlashChatTimesUsedUp = false;
        try {
            List<ChatterOnlineListMatchRecordEntity> chatterOnlineListMatchRecordEntities = chatterOnlineListMatchRecordDao.selectTodayByChatter(chatterUserCode);
            int todayCount = 0;
            if (CollectionUtils.isNotEmpty(chatterOnlineListMatchRecordEntities)) {
                todayCount = chatterOnlineListMatchRecordEntities.stream().mapToInt(ChatterOnlineListMatchRecordEntity::getUsedCnt).sum();
            }

            if (chatterMaxSendFlashchatSwitch) {
                isSendFlashChatTimesUsedUp = todayCount >= chatterMaxSendFlashchatPeriod;
            }
        } catch (Exception e) {
            log.error("==getUsers error  ==,chatterCode:{}", chatterUserCode, e);
        }

        List<UserDialogDto> userDialogDtoList = new ArrayList<>();
        if (!isSendFlashChatTimesUsedUp) {
            userDialogDtoList = userService.getUsers(chatterUserCode);
        }

        if (CollectionUtils.isEmpty(userDialogDtoList)) {
            return UserRes.toUserRes(userDialogDtoList);
        }

        //【meboo-v3.1.3】加入【加白账号库】的账号，Mechat端的在线用户列表看不到该账号
        Response<List<String>> whiteListRes = whiteListService.userCodeList();
        List<String> whiteList = whiteListRes.getData();
        if (CollectionUtils.isEmpty(whiteList)) {
            return UserRes.toUserRes(userDialogDtoList);
        }

        userDialogDtoList = userDialogDtoList.stream().filter(item -> !whiteList.contains(item.getUserCode())).collect(Collectors.toList());
        return UserRes.toUserRes(userDialogDtoList);
    }

    @Override
    public List<ModelRes> getModels(String chatterUserCode, String userCode) {
        List<ModelDto> modelDtoList = userService.getModels(chatterUserCode, userCode);
        return ModelRes.toModelRes(modelDtoList);
    }

    @Override
    public Result canConnect(MessageReq messageReq) {
        Result result = Result.ok();
        //判断用户是否在用户池里面
        boolean canConnect = userService.canConnect(messageReq.getUserCode());
        if (!canConnect) {
            //不存在的话 进行提示
            result = Result.build(90001, "He's  busy Now");
        }
        return result;
    }
}
